



global ROOT ""
global data "$ROOT/Data"
global dofiles "$ROOT/DoFiles"
global tables "$ROOT/Tables"
global figures "$ROOT/Figures"

u "$data\analysisfinal.dta", clear



gen treat = treatment_o<4
gen ifeedonly_o = 1 if treatment_o==1
replace ifeedonly_o = 0 if treatment_o>1 & treatment_o<.

gen wgonly_o = 1 if treatment_o==2
replace wgonly_o = 0 if treatment_o==1 | treatment_o==3 | treatment_o==4

keep if treatment_o==2 | treatment_o==4		/* Keeping on wg only and control zones */


/*Creating variable of Head or Spouse's main activity is agriculture */

gen        head_or_spouse_agri=1 if head_agri==1|spouse_agri==1
replace    head_or_spouse_agri=0 if head_agri==0&spouse_agri==0

*Secondary occupation...
tab head_or_spouse_agri crop 
sum inc_crop if head_or_spouse_agri==1&crop==1, d
sum inc_crop if head_or_spouse_agri==0&crop==1, d


sum crop inc_crop share_new, d
sum crop inc_crop share_new if wave==1, d
sum crop inc_crop share_new if wave==2, d

sum crop inc_crop share_new
sum crop inc_crop share_new if wave==1
sum crop inc_crop share_new if wave==2





//*Adding for dshock
cd "$risk_res"
cap erase "dshock_interv.txt"
global outregopts "addstat("Intracluster Correlation", r(rho)) bracket"
reg dcrop wave wgonly_o, cluster(zone_o)
loneway crop zone_o
outreg2 using "shock_interv.txt", keep(wgonly_o) $outregopts ctitle("dcrop") title("Incidence of Crop Loss, By Intervention")
reg dcrop wave wgonly_o, cluster(zone_o)
bootwildct wave wgonly_o

reg dinc_crop wave wgonly_o, cluster(zone_o)
loneway dinc_crop zone_o
outreg2 using "shock_interv.txt", keep(wgonly_o) $outregopts ctitle("dinc_crop") 
reg dinc_crop wave wgonly_o, cluster(zone_o)
bootwildct wave wgonly_o

cap drop tt tt2
qui reg dinc_crop wave wgonly_o, cluster(zone_o)
gen tt = e(sample)
bys num: egen tt2 = max(tt)
bys wave: sum inc_crop if tt2==1

reg dintensity wave wgonly_o, cluster(zone_o)
loneway dintensity zone_o
outreg2  using "shock_interv.txt", keep(wgonly_o) $outregopts ctitle("dintensity") 
reg dintensity wave wgonly_o, cluster(zone_o)
bootwildct wave wgonly_o

cap drop tt tt2
qui reg dintensity wave wgonly_o, cluster(zone_o)
gen tt = e(sample)
bys num: egen tt2 = max(tt)
bys wave: sum intensity if tt2==1

bys wave: sum intensity if crop==1 & tt2==1
drop tt tt2




local village "vill1"
/* Checking how idiosyncratic crop loss shocks are */

preserve
collapse crop inc_crop dcrop dinc_crop (count) num (sd) sd_crop = crop sd_inc = inc_crop , by(`village' wave)

su crop if wave==1 & num>1, det
count if crop>0 & crop<1 & wave==1 & num>1
su dcrop if wave==2 & num>1, det
count if dcrop~=0 & num>1 & wave==2
count if dcrop~=-1 & dcrop~=0 & dcrop~=1 & num>1 & wave==2
su inc_crop if wave==1 & num>1, det

su crop if wave==2 & num>1, det
count if crop>0 & crop<1 & wave==2 & num>1
su inc_crop if wave==1 & num>1, det

histogram crop if wave==1 & num>1, ytitle(Density) xtitle(Proportion of households with crop shock in village) title(Wave1)
graph save "$figures\hist_crop1.gph", replace
histogram crop if wave==2 & num>1, ytitle(Density) xtitle(Proportion of households with crop shock in village) title(Wave2)
graph save "$figures\hist_crop2.gph", replace
histogram dcrop if wave==2 & num>1
graph save "$figures\hist_dcrop.gph", replace


graph combine "$figures\hist_crop1.gph" "$figures\hist_crop2.gph"
graph save "$figures\hist_crop_comb.gph", replace


restore



/* Restricting to the sample in which either Head or Spouse's main activity is agriculture */

preserve
keep if head_or_spouse_agri==1

collapse crop inc_crop dcrop dinc_crop (count) num (sd) sd_crop = crop sd_inc = inc_crop , by(`village' wave)

su crop if wave==1 & num>1, det
count if crop>0 & crop<1 & wave==1 & num>1
su dcrop if wave==2 & num>1, det
count if dcrop~=0 & num>1 & wave==2
count if dcrop~=-1 & dcrop~=0 & dcrop~=1 & num>1 & wave==2
su inc_crop if wave==1 & num>1, det

su crop if wave==2 & num>1, det
count if crop>0 & crop<1 & wave==2 & num>1
su inc_crop if wave==1 & num>1, det

histogram crop if wave==1 & num>1, ytitle(Density) xtitle(Proportion of households with crop shock in village) title(Wave1)
graph save "$figures\hist_crop1_farmers.gph", replace
histogram crop if wave==2 & num>1, ytitle(Density) xtitle(Proportion of households with crop shock in village) title(Wave2)
graph save "$figures\hist_crop2_farmers.gph", replace
histogram dcrop if wave==2 & num>1
graph save "$figures\hist_dcrop_farmers.gph", replace


graph combine "$figures\hist_crop1_farmers.gph" "$figuresh\hist_crop2_farmers.gph"
graph save "$figures\hist_crop_comb_farm.gph", replace

restore
